package com.yunrui.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunrui.device.common.Result;
import com.yunrui.device.entity.Cabinet;
import com.yunrui.device.entity.CabinetSlot;
import com.yunrui.device.service.CabinetService;
import com.yunrui.device.service.CabinetSlotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "柜机管理接口")
@RestController
@RequestMapping("/cabinet")
public class CabinetController {

    @Autowired
    private CabinetService cabinetService;

    @Autowired
    private CabinetSlotService cabinetSlotService;

    @ApiOperation("新增柜机（自动生成插槽）")
    @PostMapping("/add")
    public Result<String> add(@RequestBody Cabinet cabinet) {
        boolean saved = cabinetService.saveWithSlots(cabinet);
        return saved ? Result.success("添加成功") : Result.error("添加失败");
    }

    @ApiOperation("查询所有柜机")
    @GetMapping("/list")
    public Result<List<Cabinet>> list() {
        List<Cabinet> list = cabinetService.list();
        return Result.success(list);
    }

    @ApiOperation("删除柜机")
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        boolean removed = cabinetService.removeById(id);
        return removed ? Result.success("删除成功") : Result.error("删除失败");
    }

    @ApiOperation("查看某个柜机的所有插槽")
    @GetMapping("/{cabinetId}/slots")
    public Result<List<CabinetSlot>> getSlots(@PathVariable Long cabinetId) {
        List<CabinetSlot> slots = cabinetSlotService.list(
                new QueryWrapper<CabinetSlot>().eq("cabinet_id", cabinetId)
        );
        return Result.success(slots);
    }
}
